package phoenix.web.page_objects;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;

import phoenix.core.PhoenixManager;
import phoenix.core.base.extensions.PageBaseWebExt;
import phoenix.core.config.extensions.ConfigExt;
import phoenix.core.data_models.TestUserDataModel;
import phoenix.web.locators.base.MyAccountLocatorsBase;
import phoenix.web.test_objects.data_model.NotificationsAlerts;

public class MyAccountPage extends PageBaseWebExt
{
	/**
	 * Initializes the SearchPage
	 * @param p_manager PhoenixManager instance
	 */
	public MyAccountPage(PhoenixManager p_manager)
	{
		super(p_manager);
	}

	/**
	 * Gets web elements by site
	 * @return IndexPageLocatorsBase instance
	 */
	public MyAccountLocatorsBase elements()
	{
		switch (manager.testModel().getSiteName())
		{
			case ConfigExt.SITE_ID_CLICKANDFLIRT:
			{
				return new phoenix.web.locators.clickandflirt.MyAccountLocators();
			}
			case ConfigExt.SITE_ID_LOCALSGOWILD:
			{
				return new phoenix.web.locators.localsgowild.MyAccountLocators();
			}
			case ConfigExt.SITE_ID_EBONYFLIRT:
			{
				return new phoenix.web.locators.ebonyflirt.MyAccountLocators();
			}
			case ConfigExt.SITE_ID_CHEECKYLOVERS:
			{
				return new phoenix.web.locators.cheekylovers.MyAccountLocators();
			}
			case ConfigExt.SITE_ID_WILDBUDDIES:
			{
				return new phoenix.web.locators.wildbuddies.MyAccountLocators();
			}
			case ConfigExt.SITE_ID_FLIRT:
			{
				return new phoenix.web.locators.flirt.MyAccountLocators();
			}
			case ConfigExt.SITE_ID_FOREIGNGIRLFRIEND:
			{
				return new phoenix.web.locators.foreigngirlfriend.MyAccountLocators();
			}
			case ConfigExt.SITE_ID_BENAUGHTY:
			{
				return new phoenix.web.locators.benaughty.MyAccountLocators();
			}
			case ConfigExt.SITE_ID_NAUGHTYFEVER:
			{
				return new phoenix.web.locators.naughtyfever.MyAccountLocators();
			}
			case ConfigExt.SITE_ID_MATUREDATING:
			{
				return new phoenix.web.locators.maturedating.MyAccountLocators();
			}
			default:
			{
				return null;
			}
		}
	}

	public void waitForPageLoad()
	{
		wd().waitForVisibility(elements().elementMySettingsDiv, "My Account block");
	}

	/**
	 * Password field
	 */

	public void setOldPassword(String p_value)
	{
		wd().waitForPresence(elements().inputOldPass, "New Password field");
		wd().type(elements().inputOldPass, p_value);
	}

	public void setNewPassword(String p_value)
	{
		wd().waitForPresence(elements().inputNewPass, "OLD Password field");
		wd().type(elements().inputNewPass, p_value);
	}

	/**
	 * Click on button 'EditPassword'
	 */
	public void clickBtnEditPass()
	{
		wd().waitForPresence(elements().buttonEditPassword, "Button 'EditPassword' doesn't appear");
		wd().click(elements().buttonEditPassword);
	}

	public void submitPassword()
	{
		wd().waitForVisibility(elements().buttonSavePassword, "Save Password");
		wd().click(elements().buttonSavePassword);
	}

	/**
	 * Typw old and new password on basic form
	 */
	public void fillPasswordForm(String p_oldPassword, String p_newPassword)
	{
		log().info("Input 'NewPass' was displayed.");

		this.setNewPassword(p_newPassword);
		this.setOldPassword(p_oldPassword);
	}

	public void waitForPasswordSuccess()
	{
		wd().waitForVisibility(elements().blockSuccessPasswordMessage, "Check for success message for password change");
	}

	/**
	 * field Email
	 */

	public void clickEditEmail()
	{
		wd().waitForPresence(elements().buttonEditEmail, "Button 'EditEmail' doesn't appear");
		log().info("Button 'EditEmail' was displayed. Click on this button...");
		wd().scrollWithOffset(wd().getWebElement(elements().buttonEditEmail), 0, -200);
		wd().click(elements().buttonEditEmail);
	}

	public void setEmailPassword(String p_value)
	{
		wd().waitForPresence(elements().inputPassword, "Password field");
		wd().scrollWithOffset(wd().getWebElement(elements().inputPassword), 0, -200);
		wd().wait(1);
		wd().type(elements().inputPassword, p_value);
	}

	public void setEmailNew(String p_value)
	{
		wd().waitForPresence(elements().inputEmail, "Email field");
		wd().scrollWithOffset(wd().getWebElement(elements().inputEmail), 0, -200);
		wd().wait(1);
		wd().type(elements().inputEmail, p_value);
	}

	public void submitEmail()
	{
		wd().waitForPresence(elements().buttonSaveEmail, "Button 'EditEmail' doesn't appear");
		wd().click(elements().buttonSaveEmail);
	}

	public void fillEmailForm(String p_newEmail, String p_password)
	{
		this.setEmailPassword(p_password);
		this.setEmailNew(p_newEmail);
	}

	public void waitForEmailSuccess()
	{
		wd().waitForVisibility(elements().elementSuccessMessage, "Check for success message for email change");
	}

	public void clickUpgrade()
	{
		log().info("MyAccount Upgrade package button redirect on payment page");
		wd().waitForPresence(elements().btnMembership, "Upgrade button");
		wd().click(elements().btnMembership);
	}

	public void waitForConfirmationMessage(String p_email)
	{
		wd().waitForPresence(elements().blockRemindPasswordMessage, "Reming password");

		if (wd().getText(elements().blockRemindPasswordMessage).contains("has been sent"))
		{
			log().info("Message has been sent to " + p_email);
		}
		else
		{
			log().warn("Can not find successful message " + p_email);
		}
	}

	public void clickDeactivateSubscribition()
	{
		wd().waitForPresence(elements().btnDeactSubscr, "Desctivate Subscribition");
		wd().click(elements().btnDeactSubscr);
	}

	public void clickRemindPassword()
	{
		wd().waitForPresence(elements().btnRemindPassword, "Remind password");
		wd().click(elements().btnRemindPassword);
	}

	public void clickRemoveAccount()
	{
		wd().waitForPresence(elements().btnRemoveAccount, "Remove Account");
		wd().click(elements().btnRemoveAccount);
	}

	public void clickBillingHistory()
	{
		wd().waitForPresence(elements().btnBillingHistory, "Billing History");
		wd().click(elements().btnBillingHistory);
	}

	public void clickButtonUpgrade()
	{
		wd().waitForPresence(elements().btnMembership, "button Upgrade");
		wd().click(elements().btnMembership);
	}

	/**
	 * TODO Will be implemented via POST requests
	 * @param params
	 */
	public void setMatchNotifications(NotificationsAlerts params)
	{
		wd().openPage("/account");
		wd().waitForVisibility(elements().blockMySettings, "Block My Settings check");
		wd().wait(2);
		// Make clicks
		this.checkboxAction(this.getMatchParams(params));
	}

	private Map<By, Boolean> getMatchParams(NotificationsAlerts params)
	{
		Map<By, Boolean> parametersMap = new HashMap<By, Boolean>();

		parametersMap.put(elements().checkboxYouMayLike, params.bYouMayLike);
		parametersMap.put(elements().checkboxAddedANewPhoto, params.addedNewPhoto);
		parametersMap.put(elements().checkboxUpgradedProfile, params.upgradedProfiles);
		parametersMap.put(elements().checkboxAddedDescriptionDetails, params.addedDescriptionDetails);
		parametersMap.put(elements().checkboxAddedPersonalDetails, params.addedPersonalDetails);
		parametersMap.put(elements().checkboxRecentlyJoinedTheSite, params.recentlyJoinedSite);
		parametersMap.put(elements().checkboxOnlineNow, params.onlineNow);

		return parametersMap;
	}

	/**
	 * TODO Will be implemented via POST requests
	 * @param parametersMap
	 */
	private void checkboxAction(Map<By, Boolean> parametersMap)
	{
		Integer pauseTimeout = 1;
		String checkboxStatus;
		String checkboxName = null;

		JavascriptExecutor jse = (JavascriptExecutor) wd().driver();
		Iterator<Entry<By, Boolean>> iterator = parametersMap.entrySet().iterator();

		while (iterator.hasNext())
		{
			Map.Entry<By, Boolean> pairs = iterator.next();

			By keyLocator = pairs.getKey();
			Boolean valueStatus = pairs.getValue();

			wd().waitForPresence(keyLocator, "keyLocator=" + keyLocator);
			wd().waitForPresence(keyLocator, "keyLocator=" + keyLocator);

			checkboxStatus = wd().getAttribute(keyLocator, "checked");

			if (checkboxStatus == null)
			{
				checkboxStatus = "false";
			}

			log().info(checkboxName + " is " + checkboxStatus);

			if (checkboxStatus.equals("false") && valueStatus == true)
			{
				wd().waitForPresence(keyLocator, "keyLocator=" + keyLocator);
				wd().scrollToBy(keyLocator, 5, false);
				wd().wait(pauseTimeout);
				wd().clickElementByJS(keyLocator);
				wd().wait(pauseTimeout);
				continue;
			}

			if (checkboxStatus.equals("true") && valueStatus == false)
			{
				wd().waitForPresence(keyLocator, null);
				wd().scrollToBy(keyLocator, 5, false);
				wd().wait(pauseTimeout);
				wd().clickElementByJS(keyLocator);
				wd().wait(pauseTimeout);
				continue;
			}
		}
	}

	/**
	 * Helpers
	 * Don't use for test
	 * @param p_user
	 */
	public void disableMatchNotifications(TestUserDataModel p_user)
	{
		// Disable match notifications
		wd().openURL(p_user.getAutologin());
		NotificationsAlerts params = new NotificationsAlerts();
		params.disableMatchAlerts();
		manager.web().myAccountPage().setMatchNotifications(params);
	}

	/**
	 * * Helpers
	 * Don't use for test
	 * @param p_user
	 */
	public void disableAllNotifications(TestUserDataModel p_user)
	{
		// Disable match notifications
		wd().openURL(p_user.getAutologin());
		NotificationsAlerts params = new NotificationsAlerts();
		params.disableAllPopUps();
		manager.backend().notificationsSettingsPage().setNotifications(manager.testUser(), params);
		manager.web().myAccountPage().setMatchNotifications(params);
	}
}